﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace TeamWork_Client
{
    public partial class Connect : Form
    {
        public Connect()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new RCon(textBox1.Text, textBox3.Text, "", long.Parse(textBox4.Text)).Show();
            this.Hide();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new RegisterSub().ShowDialog();
        }

        string Getstringfrombytes(byte[] bts)
        {
            string s = "";
            foreach(var b in bts)
            {
                s += b.ToString("X");
            }
            return s;
        }

        private void Connect_Load(object sender, EventArgs e)
        {
            if (!File.Exists("nickname.txt"))
            {
                byte[] bts = new byte[5];
                new Random().NextBytes(bts);
                File.WriteAllText("nickname.txt", "user_" + Getstringfrombytes(bts)); //6F86D0B7
            }
        }

        private void commandLink1_Click(object sender, EventArgs e)
        {
            try
            {
                RCon rc = new RCon(textBox1.Text, textBox3.Text, "", long.Parse(textBox4.Text), true);
                if (rc.Authorize())
                {
                    new Form1(rc.ssa).Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Error during vailidating given credentials.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Oops! Something went wrong. Please make sure that you've filled in the right information or that your connection is up.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
